<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>超市</title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<style type="text/css">
			ul,
			li {
				padding: 0;
				margin: 0;
			}

			.mui-row.mui-fullscreen>[class*="mui-col-"] {
				height: 100%;
			}

			.mui-col-xs-3,
			.mui-control-content {
				overflow-y: auto;
				height: 100%;
			}

			.mui-segmented-control .mui-control-item {
				line-height: 50px;
				width: 100%;
			}

			.mui-segmented-control.mui-segmented-control-inverted .mui-control-item.mui-active {
				background-color: #fff;
			}

			.second {}

			.second:hover {
				background-color: skyblue;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">超市</h1>
		</header>
		<div class="mui-content mui-row mui-fullscreen" id="shopContent">
			<div class="mui-col-xs-3">
				<div id="segmentedControls" class="mui-segmented-control mui-segmented-control-inverted mui-segmented-control-vertical">
					<div :class="['mui-control-item',{'mui-active':index == 0}]" v-for="(item,index) in list" v-if="item.isParent == true">
						<a :class="['mui-control-item',{'mui-active':index == 0}]" v-bind="{ href:'#item'+item.catBasicFid }">{{item.catBasicFid}}--{{item.name}}</a>
						<ul>
							<li class="second" v-for="i in list" v-if="i.catBasicPid == item.catBasicFid">
								<a v-bind="{ href:'#item'+i.catBasicFid }">{{i.catBasicFid}}--{{i.name}}</a>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<div id="segmentedControlContents" class="mui-col-xs-9" style="border-left: 1px solid #c8c7cc;">
				<div v-for="(itemList,index) in Rlists" v-bind="{ id:'item'+itemList.smenuid }" :class="['mui-control-item',{'mui-active':index == 0}]">
					<p>{{ itemList.title }}</p>
					<p>{{ itemList.sellPoint }}</p>
					<p>{{ itemList.unit }}</p>
					<p>{{ itemList.price }}</p>
					<p>{{ itemList.stock }}</p>
				</div>
			</div>
		</div>
	</body>
	<script src="js/mui.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="js/jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
	<script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		var vm = new Vue({
			el: '#shopContent',
			data: {
				list: [],
				Rlists: []
			},
			mounted: function() {
				this.getJson();
				this.getLists();
				this.init();
			},
			methods: {
				init: function() {
					mui.init({
						swipeBack: true //启用右滑关闭功能
					});
					//var controls = document.getElementById("segmentedControls");
					//var contents = document.getElementById("segmentedControlContents");
					//默认选中第一个
					//controls.querySelector('.mui-control-item').classList.add('mui-active');
					//contents.querySelector('.mui-control-content').classList.add('mui-active');
					var controlsElem = $("#segmentedControls");
					var contentsElem = $("#segmentedControlContents");
					var controlListElem = controlsElem.find('a.mui-control-item');
					var contentListElem = contentsElem.find('.mui-control-content');
					var controlWrapperElem = controlsElem.parentNode;
					var controlWrapperHeight = controlWrapperElem.offsetHeight;
					var controlMaxScroll = controlWrapperElem.scrollHeight - controlWrapperHeight; //左侧类别最大可滚动高度
					console.log(controlsElem)
					var maxScroll = contentsElem.scrollHeight - contentsElem.offsetHeight; //右侧内容最大可滚动高度
					var controlHeight = controlListElem[0].offsetHeight; //左侧类别每一项的高度
					var controlTops = []; //存储control的scrollTop值
					var contentTops = [0]; //存储content的scrollTop值
					var length = contentListElem.length;
					for(var i = 0; i < length; i++) {
						controlTops.push(controlListElem[i].offsetTop + controlHeight);
					}
					for(var i = 1; i < length; i++) {
						var offsetTop = contentListElem[i].offsetTop;
						if(offsetTop + 100 >= maxScroll) {
							var height = Math.max(offsetTop + 100 - maxScroll, 100);
							var totalHeight = 0;
							var heights = [];
							for(var j = i; j < length; j++) {
								var offsetHeight = contentListElem[j].offsetHeight;
								totalHeight += offsetHeight;
								heights.push(totalHeight);
							}
							for(var m = 0, len = heights.length; m < len; m++) {
								contentTops.push(parseInt(maxScroll - (height - heights[m] / totalHeight * height)));
							}
							break;
						} else {
							contentTops.push(parseInt(offsetTop));
						}
					}
					contentsElem.addEventListener('scroll', function() {
						var scrollTop = contentsElem.scrollTop;
						for(var i = 0; i < length; i++) {
							var offsetTop = contentTops[i];
							var offset = Math.abs(offsetTop - scrollTop);
							//						console.log("i:"+i+",scrollTop:"+scrollTop+",offsetTop:"+offsetTop+",offset:"+offset);
							if(scrollTop < offsetTop) {
								if(scrollTop >= maxScroll) {
									onScroll(length - 1);
								} else {
									onScroll(i - 1);
								}
								break;
							} else if(offset < 20) {
								onScroll(i);
								break;
							} else if(scrollTop >= maxScroll) {
								onScroll(length - 1);
								break;
							}
						}
					});
					var lastIndex = 0;
					//监听content滚动
					var onScroll = function(index) {
						if(lastIndex !== index) {
							lastIndex = index;
							var lastActiveElem = controlsElem.querySelector('.mui-active');
							lastActiveElem && (lastActiveElem.classList.remove('mui-active'));
							var currentElem = controlsElem.querySelector('.mui-control-item:nth-child(' + (index + 1) + ')');
							currentElem.classList.add('mui-active');
							//简单处理左侧分类滚动，要么滚动到底，要么滚动到顶
							var controlScrollTop = controlWrapperElem.scrollTop;
							if(controlScrollTop + controlWrapperHeight < controlTops[index]) {
								controlWrapperElem.scrollTop = controlMaxScroll;
							} else if(controlScrollTop > controlTops[index] - controlHeight) {
								controlWrapperElem.scrollTop = 0;
							}
						}
					};
					//滚动到指定content
					var scrollTo = function(index) {
						contentsElem.scrollTop = contentTops[index];
					};
					mui(controlsElem).on('tap', '.mui-control-item', function(e) {
						scrollTo(this.getAttribute('data-index'));
						return false;
					});
				},
				getJson: function() {
					$.ajax({
						type: "get",
						url: "http://192.168.1.75:8080/itemcat/list",
						async: true,
						dataType: 'json',
						data: {
							shopid: 150045795431766
						},
						success: function(res) {
							console.log(res)
							vm.list = res.rows;
						},
						error: function(type) {
							console.log(type)
						}
					});
				},
				getLists: function() {
					$.ajax({
						//url: "http://192.168.1.75:8080/item/getListByFMenuId",
						url: 'data/shoplists.json',
						type: "get",
						data: {
							shopid: 150045795431766,
							fmenuid: 101
						},
						dataType: 'json',
						async: true,
						success: function(res) {
							console.log(res)
							vm.Rlists = res.rows;
						},
						error: function(type) {
							console.log(type)
						}
					});
				}
			}
		})
	</script>

</html>